import { store } from "@/store"
import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { showError } from '@/utils/msg-show'
import { getPageApi } from '@/api/sys/appVersion'

export const useAppVersionStore = defineStore("AppVersion", () => {

	const isZero = ref(true)
    const dataList = ref<any[]>([])
	const totalSize = ref()
    const query = reactive<any>({
        pageNum: 1,
        pageSize: 10,
    })

	/** 分页获取 */
	async function getPage(param: any){
		await getPageApi(param).then((res: any) => {
			totalSize.value = res.data.total
			if(param.pageNum == 1){
				dataList.value = res.data.list
			}else{
				dataList.value = dataList.value.concat(res.data.list)
			}
		}).catch(err => {
			showError(err.msg || err.errMsg || err)
		})
	}

	/** 更新 */
	async function update(){
        const size = query.pageNum * query.pageSize
		await getPage({ pageNum: 1, pageSize: size })
	}

	/** 下一页 */
    async function nextPage(){
		await getPage(query);
	}

	/** 初始化 */
	async function init() {
		if (isZero.value) {
            await getPage(query)
			isZero.value = false
		}
	}
   
	return {
		init, update, nextPage,
        dataList, query, totalSize
	}

})

export function useAppVersionStoreHook() {
	return useAppVersionStore(store);
}